﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Passwords.ViewModels;
using System.Collections.ObjectModel;

namespace Passwords.Models
{
    /// <summary>
    /// Used to display a list of categories and determine whose attached to a given password.
    /// </summary>
    public class SelectedCategory : Entity
    {
        private bool isSelected;
        private CategoryContract category;

        public SelectedCategory(CategoryContract category, bool isSelected)
        {
            this.isSelected = isSelected;
            this.category = category;
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        public CategoryContract Category { get { return category; } }

        public string Name { get { return category.Name; } }


        public static ObservableChildCollection<SelectedCategory> GetCollection(DataViewModel viewModel, PasswordContract password)
        {
            var passwordIdDict = password.Categories.ToDictionary(c => c.Id, null);
            var src = viewModel.Categories.Select(c => new SelectedCategory(c, passwordIdDict.ContainsKey(c.Id ?? string.Empty)));
            ObservableChildCollection<SelectedCategory> result = new ObservableChildCollection<SelectedCategory>(password, src);
            return result;
        }

    }
}
